package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Jrjbxx;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class JrjbxxForm extends BasePage implements Serializable {
    private GenericManager<Jrjbxx, Long> jrjbxxManager;
    private Jrjbxx jrjbxx = new Jrjbxx();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setJrjbxxManager(GenericManager<Jrjbxx, Long> jrjbxxManager) {
        this.jrjbxxManager = jrjbxxManager;
    }

    public Jrjbxx getJrjbxx() {
        return jrjbxx;
    }

    public void setJrjbxx(Jrjbxx jrjbxx) {
        this.jrjbxx = jrjbxx;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        jrjbxxManager.remove(jrjbxx.getId());
        addMessage("jrjbxx.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            jrjbxx = jrjbxxManager.get(id);
        } else {
            jrjbxx = new Jrjbxx();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (jrjbxx.getId() == null || jrjbxx.getId() == 0);
        jrjbxx.setJbqk(jbqk);
     // set username who act this operation, get the user name from httprequest
        jrjbxx.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        jrjbxx.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        jrjbxx.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        jrjbxxManager.save(jrjbxx);

        String key = (isNew) ? "jrjbxx.added" : "jrjbxx.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	// set the id to the next page
            this.id = jrjbxx.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 